package dart

class Round {

	List attempts = new ArrayList()

	static belongsTo = [playerGame: PlayerGame]
	static hasMany = [attempts: Attempt]
	static transients = ['score']

	Float getScore(Float startingScore) {
		Game game = playerGame.game
		Float result = startingScore
		Float lastValidResult = result;
		
		attempts.each {Attempt attempt ->
			result += (game.countDown ? -attempt.score : attempt.score);

			if ( game.isBeyondLimit(result) )
				result = game.scoreLimit;

			if ( game.isBeyondBounceBack(result) ){
				result = 2 * game.bounceBack - result;
			}else if ( game.isBust(result) )
				result = lastValidResult;

			lastValidResult = result;
		}

		return result;
	}

	Boolean isBust(Float startingScore) {
		Game game = playerGame.game
		Float result = startingScore;

		attempts.each {Attempt attempt ->
			result += (game.countDown ? -attempt.score : attempt.score);
		}

		return game.isBust(result)
	}
}